package leetcode.problems;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by gmwang on 2018/7/25
 * 唯一的email地址
 */
public class _0820UniqueEmailAddresses {
    /**
     * Every email consists of a local name and a domain name, separated by the @ sign.
     * 每封电子邮件都有一个本地名称和一个域名，由@符号分隔。
     * For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.
     * 例如，在alice@leetcode.com中，alice是本地名，而leetcode.com是域名。
     * Besides lowercase letters, these emails may contain '.'s or '+'s.
     * 除了小写字母外，这些电子邮件可能包含''.'或'+'。
     * If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.  (Note that this rule does not apply for domain names.)
     * 如果在电子邮件地址的本地名称部分中的某些字符之间添加句点('.')，则发送给该地址的邮件将被转发到本地名称中没有点的地址。例如，“alice.z@leetcode.com”和“alicez@leetcode.com”转发到同一个电子邮件地址。（注意，此规则不适用于域名。）
     * If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com.  (Again, this rule does not apply for domain names.)
     * 如果在本地名称中加上“+”（“+”），则第一个加号后面的所有内容都将被忽略。这允许某些电子邮件被过滤，例如m.y+name@email.com将被转发到my@email.com网站。（同样，这个规则不适用于域名。）
     * It is possible to use both of these rules at the same time.
     * 可以同时使用这两个规则。
     * Given a list of emails, we send one email to each address in the list.  How many different addresses actually receive mails?
     * 给定一个电子邮件列表，我们发送一个电子邮件到列表中的每个地址。有多少不同的地址实际收到邮件？
     *
     * Example 1:
     *
     * Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
     * Output: 2
     * Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails
     *
     *
     * Note:
     *
     * 1 <= emails[i].length <= 100
     * 1 <= emails.length <= 100
     * Each emails[i] contains exactly one '@' character.
    /**
     *
     * @param emails
     * @return
     */
    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        //@分隔
        for (int i = 0; i < emails.length; i++) {
            String email = emails[i];
            String[] twoName = email.split("@");
            String local = twoName[0];
            local = local.replaceAll("\\.","");
            if(local.contains("+")){
                local = local.split("\\+")[0];
            }
            String domain = twoName[1];
            set.add(local+domain);
        }
        return set.size();
    }
    @Test
    public void test() {
        String[] ints = {"test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"};
        int res = numUniqueEmails(ints);
        System.out.println(res);
    }
}
